const request = require('../../utils/request')
Page({
  data: {
    background: ['1', '2', '3'],
    interval: 2000,
    images: ["/assets/home/banner1.jpg", "/assets/home/banner2.jpg", "/assets/home/banner3.jpg"],
    showIndex: false,
    visible: false,
    closeBtn: true,
    scriptList: [],
    systemSetting: {},
    defaultImage: 'https://img1.baidu.com/it/u=2559867097,3726275945&fm=253&fmt=auto&app=138&f=JPEG?w=1333&h=500',
    showQr:false,
    englishTitle:'',
    chineseTitle:'',
    nowQrcode:''
  },
  handleMore() {
    wx.switchTab({
      url: '/pages/script/script',
    })
  },
  onLoad() {
    this.getSetting()
    this.getScript()
    // this.getFocusStatus()
  },
  getScript() {
    const that = this
    const token = wx.getStorageSync('token')
    request({
      url: '/script',
      data: {
        currPage: 1,
        pageSize: 4,
        token:token || ''
      },
      success(data) {
        data.data.records.forEach(item => {
          item.isLike = false
          item.scriptType = item.scriptType.split(',')
        });
        that.setData({
          scriptList: data.data.records,
        })
      },
      fail(err) {
        console.log('请求失败：', err);
      }
    })
  },
  getSetting() {
    let that = this
    var regex = /\b[a-zA-Z]+\b/g;  // 匹配英文
    var regex1 = /[\u4e00-\u9fa5]*/g;  // 匹配中文
    request({
      url: '/setting',
      success(data) {
        console.log('请求系统配置成功：', data);
        that.setData({
          systemSetting: data.data,
          englishTitle:data.data.shopName.match(regex)[0],
          chineseTitle:data.data.shopName.match(regex1).join('')
        })
        // console.log('请求偷：', that.data,);
      },
      fail(err) {
        console.log('请求失败：', err);
      }
    })

  },
  // getFocusStatus() {
  //   const userinfo = JSON.parse(wx.getStorageSync('userinfo'))
  //   const token = wx.getStorageSync('token')
  //   if (userinfo && token) {
  //     request({
  //       url: `/user/follow/${userinfo.userId}`,
  //       success(data) {
  //         console.log('获取到关注状态',data);
  //       },
  //       fail(err) {
  //         console.log('请求失败：', err);
  //       }
  //     })
  //   }
  // },
  onClick() {
    var imgUrl = this.data.images;
    console.log('imgClick success', imgUrl)
    wx.previewImage({
      urls: imgUrl, //imgUrl 必须是需要预览的图片链接列表，只有一张图片也需要是列表
      success: (res => {
        console.log('接口调用成功', res)
      })
    })
  },
  handleSearch() {
    wx.setStorageSync('isHome', true)
    wx.switchTab({
      url: '/pages/script/script',
    })
  },
  likeClick() {
    console.log('点击爱心');
  },
  handleLocation() {
    wx.openLocation({
      latitude: 30.189756,
      longitude: 120.227891,
      name: '柴火屋剧游',
      address: '浙江省杭州市滨江区西兴街道西兴园6幢516室',
      scale: 18
    })
  },
  handleCall() {
    wx.makePhoneCall({
      phoneNumber: this.data.systemSetting.mobile, //此号码仅用于测试
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  handleWifi() {
    let that = this
    wx.setClipboardData({
      data: that.data.systemSetting.wifiPassword,
      success: function (res) {
        wx.showToast({
          title: `复制密码成功\r\nWIFI名为${that.data.systemSetting.wifiAccount}`,
          icon: 'none',
          duration: 2000
        });
      },
      fail: function (err) {
        console.log('复制失败', err);
      }
    });
  },
  handleFocus(){
    console.log('show');
    this.setData({
      showQr:true,
      nowQrcode:this.data.systemSetting.qrCode
    })
  },
  handleFocus1(){
    console.log('show1');
    this.setData({
      showQr:true,
      nowQrcode:this.data.systemSetting.wxCode
    })
  },
  handleHind(){
    this.setData({
      showQr:false
    })
  },
  lookImage() {
    var imgUrl = this.data.nowQrcode;
    wx.previewImage({
        current: imgUrl, // 当前显示图片的http链接
        urls: [imgUrl] // 需要预览的图片http链接列表
    })
},
  onShareAppMessage(){},
  onShareTimeline(){}
});